package game.module.club.logic;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.entity.PlayingRole;
import game.module.club.dao.ClubRoomCache;
import game.module.ddz.logic.DdzDataManager;
import game.module.hall.bean.BaseRoom;
import game.module.hall.logic.QipaiConstants;
import game.module.hall.logic.QipaiDataManager;
import game.module.mj.logic.MjDataManager;
import game.module.nn.logic.NnDataManager;
import game.module.offline.bean.PlayerOfflineCache;
import game.module.offline.logic.PlayerOfflineManager;
import game.module.qzmj.logic.QzmjDataManager;
import game.module.sss.logic.SssDataManager;
import game.module.user.bean.PlayerBean;
import game.module.user.dao.PlayerDao;
import game.session.SessionManager;
import lua.LuaMessageHall.ClubRoom;
import lua.LuaMessageHall.S2CClubPushRoom;

/**
 * 俱乐部房
 * 
 * @author:Pomelo.lee
 * @date:2017年9月20日 下午6:45:41
 */
public class ClubRoomManager {

	private static Logger logger = LoggerFactory.getLogger(ClubRoomManager.class);

	static class SingletonHolder {
		static ClubRoomManager instance = new ClubRoomManager();
	}

	public static ClubRoomManager getInstance() {
		return SingletonHolder.instance;
	}

	/**
	 * 创建房间是否开启俱乐部房调用
	 * 
	 * @param pr
	 * @param room
	 */
	public void openClubRoom(PlayingRole pr, BaseRoom room) {
		if (room == null || room.getIs_club_room() != 1 || pr == null) {
			return;
		}
		if (pr.getPlayerBean().getClub_id() <= 0) {
			logger.error("not found clubId,unable create clubRoom,roomId={},playerId={}", room.getUuid(), pr.getId());
			return;
		}
		int clubId = pr.getPlayerBean().getClub_id();
		Byte roomType = QipaiDataManager.getInstance().getRoomType(room.getUuid());
		if (roomType == null) {
			logger.error("not found roomType,unable create clubRoom,roomId={},playerId={}", room.getUuid(), pr.getId());
		}
		ClubRoomCache.getInstance().addClubRoom(clubId, room.getUuid(), roomType);
	}

	/**
	 * 推送俱乐部房
	 * 
	 * @param pr
	 */
	public void pushClubRoom(PlayingRole pr) {
		if (pr.getPlayerBean().getClub_id() <= 0) {
			return;
		}
		int clubId = pr.getPlayerBean().getClub_id();
		Map<String, Byte> clubRoomMap = ClubRoomCache.getInstance().listClubRoomByClubId(clubId);
		if (clubRoomMap == null || clubRoomMap.size() <= 0) {
			return;
		}
		List<ClubRoom> list = new ArrayList<>();
		for (String roomId : clubRoomMap.keySet()) {
			byte roomType = clubRoomMap.get(roomId);
			ClubRoom clubRoom = new ClubRoom();
			clubRoom.room_type = roomType;
			clubRoom.room_id = roomId;
			BaseRoom baseRoom = null;
			switch (roomType) {
			case QipaiConstants.ROOM_TYPE_DDZ:
				baseRoom = DdzDataManager.getInstance().getDdzRoom(roomId);
				break;
			case QipaiConstants.ROOM_TYPE_NN:
				baseRoom = NnDataManager.getInstance().getNnRoom(roomId);
				break;
			case QipaiConstants.ROOM_TYPE_SSS:
				baseRoom = SssDataManager.getInstance().getSssRoom(roomId);
				break;
			case QipaiConstants.ROOM_TYPE_MJ:
				baseRoom = MjDataManager.getInstance().getMjRoom(roomId);
				break;
			case QipaiConstants.ROOM_TYPE_MJ_QZ:
				baseRoom = QzmjDataManager.getInstance().getMjRoom(roomId);
				break;
			default:
				break;
			}
			if (baseRoom == null) {
				logger.info("have already closed roomId={}", roomId);
				clubRoomMap.remove(roomId);
				continue;
			}
			clubRoom.is_jindou = baseRoom.getIs_jindou();
			clubRoom.max_count = QipaiConstants.getMaxCountByRoomType(clubRoom.room_type);
			packClubRoom(clubRoom, baseRoom.getRoundCount(), baseRoom.getPlayerList());
			list.add(clubRoom);
		}
		if (list.size() <= 0) {
			return;
		}
		S2CClubPushRoom respMsg = new S2CClubPushRoom();
		respMsg.club_room_list = list.toArray(new ClubRoom[0]);
		pr.writeAndFlush(respMsg.build(pr.alloc()));
	}

	private void packClubRoom(ClubRoom clubRoom, short roundCount, List<Integer> ids) {
		int createPlayerId = ids.get(0);
		clubRoom.player_id = createPlayerId;
		clubRoom.round_count = roundCount;
		clubRoom.person_count = new Integer(ids.size()).shortValue();
		PlayerOfflineCache poc = PlayerOfflineManager.getInstance().getPlayerOfflineCache(createPlayerId);
		if (poc != null) {
			clubRoom.headimg = poc.getHeadimg();
			clubRoom.player_name = poc.getName();
		} else {
			PlayingRole pr = SessionManager.getInstance().getPlayer(createPlayerId);
			PlayerBean bean = null;
			if (pr != null) {
				bean = pr.getPlayerBean();
			} else {
				bean = PlayerDao.getInstance().getPlayerById(createPlayerId);
			}
			clubRoom.headimg = bean.getHeadimg();
			clubRoom.player_name = bean.getName();
		}
	}

}
